package services;

import java.util.ArrayList;

import com.damBowling.mybolos.AlquilerDetalles;
import com.damBowling.mybolos.BoleraJSONParser;
import com.damBowling.mybolos.Menu;
import com.damBowling.mybolos.R;

import models.Alquiler;
import models.AlquilerDetalle;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

public class GestNotificaciones extends Service {

	ArrayList<Alquiler> alquileres;
	String urlServer;
	String id;
	BoleraJSONParser DAO;
	NotificationManager nm;
	SharedPreferences prefs;
	boolean retardo = false;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		if(intent != null){
			urlServer = intent.getExtras().getString("server");
			id = intent.getExtras().getString("id");
			DAO = new BoleraJSONParser();
			nm =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			prefs = getSharedPreferences("ajustes", Context.MODE_PRIVATE);
			retardo = true;
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onCreate() {
		new Thread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					if(retardo){
					if(!prefs.getBoolean("servicio", true))break;
					if(executeFirstWebService()) crearAlquilerNotificacion();
						
						try {
						Thread.sleep(60000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				}
			}
		}).start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * Llama al webservice de alquileres pendientes y compbueba si los hay
	 * 
	 * @return True cuando hay alquileres pendientes, false cuando no
	 */
	public boolean executeFirstWebService() {
		boolean encontrado = false;
		alquileres = DAO.getAlquileresById(id, urlServer);
		if (alquileres.size() != 0) {
			encontrado = true;
		}
		return encontrado;
	}
	
	/**
	 * Crea la notificaci�n de Alquileres pendientes
	 */
	public void crearAlquilerNotificacion(){
		   	        //
		        //  Create your notification 
		        int icon = R.drawable.reservasbubble;
		        CharSequence tickerText = "MyBolos";
		        long when = System.currentTimeMillis();
		 
		        Notification notification = 
		            new Notification( icon, tickerText, when);
		 
		        Context context = getApplicationContext();
		        CharSequence contentTitle = "Alquileres pendientes";
		        CharSequence contentText = "Devoluci�n de material pendiente!";
		        Intent notificationIntent = 
		            new Intent(this, AlquilerDetalles.class);
		        PendingIntent contentIntent = 
		            PendingIntent.getActivity(this, 0, notificationIntent, 0);
		 
		        notification.setLatestEventInfo(
		            context, 
		            contentTitle, 
		            contentText, 
		            contentIntent);
		 
		        // 
		        //  Send the notification
		        nm.notify( 1, notification );
	}
}